package io.bluerain.service.service;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;

/**
 * Created by foredawn on 15-9-13.
 * 与Activity实时通信的Service
 */
public class MyBindService extends Service {

    boolean quit = false;
    private int count;
    private MyBinder binder;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("onBind..");
        binder = new MyBinder();
        return binder;
    }

    @Override
    public void onCreate() {
        System.out.println("onCreate..");
        super.onCreate();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (!quit) {
                    count++;
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("onStartCommand..");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        System.out.println("onUnbind..");
        return true;
    }

    @Override
    public void onDestroy() {
        System.out.println("onDestroy..");
        quit = true;
        super.onDestroy();
    }

    public class MyBinder extends Binder {
        public int getCount() {
            return count;
        }
    }
}
